package com.yunbiao.ybbusstopplate.utils.core.power;

import android.text.TextUtils;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class PowerSelectTool {
    private static final String TAG = "PowerSelectTool";
    private static PowerSelectTool powerSelectTool;

    private PowerSelectTool(){}

    public static PowerSelectTool getInstance(){
        if (powerSelectTool ==null){
            powerSelectTool =new PowerSelectTool();
        }
        return powerSelectTool;
    }

    /**
     * 索引[0]:开机时间 1,2,3,4,5,6,7;08:00  [1]:关机时间 1,2,3,4,5,6,7;22:00
     * @param timeJson
     * @return
     */
    public String[] selectPowerSettimes(String timeJson){
        String[] times=new String[2];
        String week="";
        try {
            JSONObject timeObj = new JSONObject(timeJson);
            JSONArray dataArr = timeObj.getJSONArray("data");
            if (dataArr!=null&&dataArr.length()>0){//有定时
                week = timeObj.getString("week");
                int type = timeObj.getInt("type");
                switch (type){
                    case 1://按周,多时段
                        times=selectTimeByWeek(dataArr);
                        break;
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String[] powers=new String[2];
        if (times!=null&&times.length>0){
            if (TextUtils.isEmpty(week)){
                week="1,2,3,4,5,6,7";
            }
            powers[0]=week+",;"+times[0];
            powers[1]=week+",;"+times[1];
        }else {
            powers[0]="";
            powers[1]="";
        }
        return powers;
    }

    private String[] selectTimeByWeek(JSONArray dataArr){
        Map<String, Long> onMap=new HashMap<>();
        Map<String, Long> offMap=new HashMap<>();
        try {
            for (int i = 0; i < dataArr.length(); i++) {
                JSONObject dataObj = dataArr.getJSONObject(i);
                String sTime = dataObj.getString("sTime");
                String eTime = dataObj.getString("eTime");

                long currentTimeMillis = System.currentTimeMillis();

                //开机时间集合
                String[] startTime = sTime.split(":");
                Calendar onCalendar = Calendar.getInstance();
                onCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(startTime[0]));
                onCalendar.set(Calendar.MINUTE, Integer.parseInt(startTime[1]));
                onCalendar.set(Calendar.SECOND,0);


                if (currentTimeMillis>=onCalendar.getTimeInMillis()){
                    onCalendar.add(Calendar.DAY_OF_MONTH,1);
                }
                onMap.put(sTime,onCalendar.getTimeInMillis()-currentTimeMillis);

                //关机时间集合
                String[] endTime = eTime.split(":");
                Calendar offCalendar = Calendar.getInstance();
                offCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(endTime[0]));
                offCalendar.set(Calendar.MINUTE, Integer.parseInt(endTime[1]));
                offCalendar.set(Calendar.SECOND,0);

                if (currentTimeMillis>=offCalendar.getTimeInMillis()){
                    offCalendar.add(Calendar.DAY_OF_MONTH,1);
                }
                offMap.put(eTime,offCalendar.getTimeInMillis()-currentTimeMillis);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (!onMap.isEmpty()&&!offMap.isEmpty()){
            String[] offTime = getOffTime(offMap);
            String[] onTime = getOnTime(onMap, Long.parseLong(offTime[0]));
            Log.d(TAG, "selectTimeByWeek: onTime->"+onTime[1]+", offTime->"+offTime[1]);

            String[] times = new String[2];
            times[0]=onTime[1];
            times[1]=offTime[1];

            return times;
        }

        return null;
    }

    private String[] getOnTime(Map<String, Long> timeMap, long offTime){
        String time="";
        Long currtTime=-1L;
        Map<String, Long> tempMap1=new HashMap<>();
        for (Map.Entry<String, Long> hh:timeMap.entrySet()) {
            Long value = hh.getValue();
            if (value>offTime){
                tempMap1.put(hh.getKey(),hh.getValue());
            }
        }
        for (Map.Entry<String, Long> hh:tempMap1.entrySet()) {
//            Log.d(TAG, "getTime: key->"+hh.getKey()+",value->"+hh.getValue());
            if (TextUtils.isEmpty(time)){
                time=hh.getKey();
                currtTime=hh.getValue();
            }else {
                Long value = hh.getValue();
                if (value<currtTime){
                    currtTime=value;
                    time=hh.getKey();
                }
            }
        }
        String[] array = {currtTime + "", time};
        return array;
    }

    private String[] getOffTime(Map<String, Long> timeMap){
        String time="";
        Long currtTime=-1L;
        for (Map.Entry<String, Long> hh:timeMap.entrySet()) {
//            Log.d(TAG, "getTime: key->"+hh.getKey()+",value->"+hh.getValue());
            if (TextUtils.isEmpty(time)){
                time=hh.getKey();
                currtTime=hh.getValue();
            }else {
                Long value = hh.getValue();
                if (value<currtTime){
                    currtTime=value;
                    time=hh.getKey();
                }
            }
        }
        String[] array = {currtTime + "", time};
        return array;
    }

}
